﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Logic;
using Model;

public partial class _default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindApp();
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //判读是否有上传文件
        if (fuPhone.PostedFile.ContentLength > 0)
        {
            if (Directory.Exists(Server.MapPath("~/File")) == false)//判断文件夹是否存在,若不存在则创建
            {
                Directory.CreateDirectory(Server.MapPath("~/File"));
            }
            
            
            string UploadFilePath = Server.MapPath("File\\");
            string fullName = fuPhone.PostedFile.FileName;
            string newName = DateTime.Now.Ticks.ToString() + fullName.Substring(fullName.LastIndexOf("."));
            fuPhone.SaveAs(UploadFilePath + newName);
            ViewState["fullname"] = UploadFilePath + newName;
            lblMsg.Text = "上传成功";
            
        }
        else
        {
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string appName = cbAppList.SelectedItem.Text;
        string appId = cbAppList.SelectedItem.Value;

        List<TB_App_Info> appList = new List<TB_App_Info>();
        for (int i = 0; i < cbAppList.Items.Count; i++)
        {
            ListItem item = cbAppList.Items[i];
            if (item.Selected)
            {
                TB_App_Info appInfo = new TB_App_Info()
                {
                    AppId = Convert.ToInt32(item.Value),
                    AppName = item.Text
                };
                appList.Add(appInfo);    
            }
            
        }

        int total = 0;
        int success = 0;

        List<TB_Sale_Info> saleList = SaleInfoLogic.GetAll();

        List<string> accountList = new List<string>();
        if (ViewState["fullname"] != null)
        {
            try
            {
                StreamReader sr = new StreamReader(ViewState["fullname"].ToString());
                string content = sr.ReadLine();
                while (content != null)
                {
                    total++;
                    try
                    {
                        int saleIndex = new Random().Next(saleList.Count);

                        bool suc = Save(appList, saleList[saleIndex].SaleAccount, content);
                        if (suc)
                        {
                            if (!accountList.Contains(saleList[saleIndex].SaleAccount))
                            {
                                accountList.Add(saleList[saleIndex].SaleAccount);
                            }
                            
                            success++;
                        }    
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    content = sr.ReadLine();
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        if (success > 0)
        {
            SaleInfoLogic.UpdateModel2CanRecommand(accountList);
        }

        lblMsg.Text = string.Format("共处理数据{0}个， 成功{1}个, 失败{2}个", total, success, total - success);
    }

    private bool Save(List<TB_App_Info> appList, string account, string content)
    {
        bool suc = true;
        string phone = "";
        string pwd = "";
        string[] strs = content.Split(',');
        if (strs.Length != 2)
        {
            suc = false;
        }
        else
        {
            phone = strs[0];
            pwd = strs[1];
            if (phone.Length != 11)
            {
                suc = false;
            }
            if (pwd.Length != 6)
            {
                suc = false;
            }
        }
        if (suc)
        {
            foreach (TB_App_Info appInfo in appList)
            {
                TB_Phone_Info phoneInfo = new TB_Phone_Info() { 
                    AppId = appInfo.AppId,
                    AppName = appInfo.AppName,
                    Phone = phone,
                    Pwd = pwd,
                    RefereePhone = account,
                    STime = DateTime.Now
                };
                int rel = PhoneInfoLogic.Insert(phoneInfo);
                if (rel <= 0)
                {
                    suc = false;
                }
            }
        }
        return suc;
    }

    private void BindApp()
    {
        List<TB_App_Info> appList = AppInfoLogic.GetAll();
        foreach (TB_App_Info app in appList)
        {
            ListItem item = new ListItem();
            item.Text = app.AppName;
            item.Value = app.AppId.ToString();
            item.Selected = true;
            cbAppList.Items.Add(item);
        }
    }
}